﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using ScrumTable.BL.DM.Properties;
using ScrumTable.Common;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// Specifies the mapping between the property names of a DomainObject
    /// and the business layer properties.
    /// </summary>
    public class DomainPropertyMapper
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IDictionary<string, string> _domainToPropertyMapping;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the business layer property name associated with the data layer property name.
        /// </summary>
        /// <param name="dlPropertyName">Specifies the data layer name to map to a dm property.</param>
        /// <returns>Returns the business layer property name mapped to the given data layer property.</returns>
        public string this[string dlPropertyName]
        {
            get
            {
                PreCondition.AssertNotNullOrEmpty(dlPropertyName, "dlPropertyName");
                EnsurePropertyAvailable(dlPropertyName);
                return _domainToPropertyMapping[dlPropertyName];
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DomainPropertyMapper instance, no public constructor
        /// needed.
        /// </summary>
        public DomainPropertyMapper()
        {
            _domainToPropertyMapping = new Dictionary<string, string>();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new PropertyMapping instance, no public constructor
        /// needed.
        /// </summary>
        /// <param name="dmPropertyName">Specifies the business layer property name. This is required because of update events from the back end system.</param>
        /// <param name="dlPropertyName">Specifies the data layer property name. This is required because of update events from the back end system.</param>
        public void Add(string dmPropertyName, string dlPropertyName)
        {
            PreCondition.AssertNotNullOrEmpty(dmPropertyName, "dmPropertyName");
            PreCondition.AssertNotNullOrEmpty(dlPropertyName, "dlPropertyName");

            _domainToPropertyMapping[dlPropertyName] = dmPropertyName;
        }

        /// <summary>
        /// Returns true if the current mapper contains a mapping for the given data layer property field.
        /// </summary>
        /// <param name="dlPropertyName">Specifies the name of the property field.</param>
        public bool ContainsProperty(string dlPropertyName)
        {
            return _domainToPropertyMapping.ContainsKey(dlPropertyName);
        }

        private void EnsurePropertyAvailable(string propertyName)
        {
            if (!ContainsProperty(propertyName))
            {
                throw new InvalidOperationException(string.Format(Resources.ExcDmnMapperNotDefined, propertyName));
            }
        }

        #endregion
    }
}